/*
 * Copyright (c) 2009-2012 Xilinx, Inc.  All rights reserved.
 *
 * Xilinx, Inc.
 * XILINX IS PROVIDING THIS DESIGN, CODE, OR INFORMATION "AS IS" AS A
 * COURTESY TO YOU.  BY PROVIDING THIS DESIGN, CODE, OR INFORMATION AS
 * ONE POSSIBLE   IMPLEMENTATION OF THIS FEATURE, APPLICATION OR
 * STANDARD, XILINX IS MAKING NO REPRESENTATION THAT THIS IMPLEMENTATION
 * IS FREE FROM ANY CLAIMS OF INFRINGEMENT, AND YOU ARE RESPONSIBLE
 * FOR OBTAINING ANY RIGHTS YOU MAY REQUIRE FOR YOUR IMPLEMENTATION.
 * XILINX EXPRESSLY DISCLAIMS ANY WARRANTY WHATSOEVER WITH RESPECT TO
 * THE ADEQUACY OF THE IMPLEMENTATION, INCLUDING BUT NOT LIMITED TO
 * ANY WARRANTIES OR REPRESENTATIONS THAT THIS IMPLEMENTATION IS FREE
 * FROM CLAIMS OF INFRINGEMENT, IMPLIED WARRANTIES OF MERCHANTABILITY
 * AND FITNESS FOR A PARTICULAR PURPOSE.
 *
 */

/*
 * helloworld.c: simple test application
 *
 * This application configures UART 16550 to baud rate 9600.
 * PS7 UART (Zynq) is not initialized by this application, since
 * bootrom/bsp configures it to baud rate 115200
 *
 * ------------------------------------------------
 * | UART TYPE   BAUD RATE                        |
 * ------------------------------------------------
 *   uartns550   9600
 *   uartlite    Configurable only in HW design
 *   ps7_uart    115200 (configured by bootrom/bsp)
 */

#include "platform.h"
#include "regs.h"
#include "device.h"
#include "widget.h"
#include "menu.h"
#include "eeprom.h"
#include "camera.h"
#include "stdio.h"
#include "xstatus.h"
#include "motor.h"

unsigned int* menu_fb[3];
static unsigned int* cam0_fb[3];
static unsigned int* cam1_fb[3];
static unsigned char Timestamp[6] = {19,7,7,7,7,7};
static unsigned char FusionParams[64];

extern UIState uistate;
extern EEPROM_IIC eeprom;

int main()
{
	int nI;

    init_platform();

    xil_printf("******************stag0 mem init***********************************\r\n");
    menu_fb[0] = (unsigned int*)(RESERVE_START + 0x80000*0x0);
    menu_fb[1] = (unsigned int*)(RESERVE_START + 0x80000*0x1);
    menu_fb[2] = (unsigned int*)(RESERVE_START + 0x80000*0x2);
    cam0_fb[0] = (unsigned int*)(RESERVE_START + 0x80000*0x3);
    cam0_fb[1] = (unsigned int*)(RESERVE_START + 0x80000*0x4);
    cam0_fb[2] = (unsigned int*)(RESERVE_START + 0x80000*0x5);
    cam1_fb[0] = (unsigned int*)(RESERVE_START + 0x80000*0x6);
    cam1_fb[1] = (unsigned int*)(RESERVE_START + 0x80000*0x7);
    cam1_fb[2] = (unsigned int*)(RESERVE_START + 0x80000*0x8);

    rMENU_BUF0 = (unsigned int)menu_fb[0];
    rMENU_BUF1 = (unsigned int)menu_fb[1];
    rMENU_BUF2 = (unsigned int)menu_fb[2];

    rCAM0_BUF0 = (unsigned int)cam0_fb[0];
    rCAM0_BUF1 = (unsigned int)cam0_fb[1];
    rCAM0_BUF2 = (unsigned int)cam0_fb[2];

    rCAM1_BUF0 = (unsigned int)cam1_fb[0];
    rCAM1_BUF1 = (unsigned int)cam1_fb[1];
    rCAM1_BUF2 = (unsigned int)cam1_fb[2];

	xil_printf("******************stag1 camera init********************************\r\n");
    rCAM0_CTRL = (0x80<<24|0x80 << 16); // start video
	rCAM1_CTRL = (0x80<<24|0x80 << 16); // start video
	rCAM0_PWR  = 0x101; //start cfg
	rCAM1_PWR  = 0x101; //start cfg

    xil_printf("******************stag2 disp init*************************************\r\n");
    for(nI=0;nI<(PIX_CNT);nI ++) *(menu_fb[0] + nI) = VIDEO;
    rVGA_PCNT  = PIX_CNT; // vga_pix_size
    rBACKLIGHT = 10;
    rBLACK_PIX = 0x10;
    rVGA_CTRL  = (0 << 8)|(0x1<<2)|(0x1<<1)|0x1; // masksel[15:8] start lcd disp[0], Menu enable[1],bar hide[2]

    rVIEW_XY   = 0x2;

    rPWR_CTRL  = 0x1;
    udelay(1000000);
    rPWR_CTRL  = 0x2;

    adjust_speed_set(40);
    pushing_speed_set(40);

    //next_page(0);
    x_motor_ctrl(1,1,1,0,0,0xffff);
    y_motor_ctrl(1,1,1,0,0,0xffff);

    while (1) {
        render();
        //camera0_read_id();
    	//camera1_read_id();

    	//camera0_write_i2c(0x4,012);
    	//udelay(1000000);
    	//camera0_read_i2c(0x4);
    	//get_rtc_remote_time(Timestamp);
        if(uistate.drawstage == 3 ) rVGA_CTRL = ((uistate.nextfb&0x1)<< 8)|(0x1<<2)|(0x1<<1)|0x1;
        udelay(1000);
        //camera_status_show();
    }

    set_rtc_remote_time(Timestamp);
    xil_printf("******************stag4 work start **********************************\r\n");
    while(rIICCFG_STATE != 0x10001000)  {
    	xil_printf("camera verify state=%2x\r\n",rIICCFG_STATE);
    	udelay(100000);
    }
    xil_printf("camera verify state=%2x\r\n",rIICCFG_STATE);
    //udelay(500000);
    //camera0_read_id();
	//camera1_read_id();
    xil_printf("******************stag3 aux init**************************************\r\n");
    rHEAT_CTRL = (0x0 << 31|70 << 16|45000); // manual mode,70% speed,45s
    rBASE_X    = (320 << 16)|320;
    rPIX_SENS  = (0x18 << 8)|0xa;
    rLPUSH_CFG = ((320 - 16) << 16)|(320 - 60);
    rRPUSH_CFG = ((320 + 16) << 16)|(320 + 60);
    rPUSH_CTRL = (0x1 << 24)|(0x20 << 16)|(0x5 << 8)|0x8; //pause mod3 ,view0, auto splice, push_en
    rAUX_CTRL  = 0x0;

	//eeprom.write_byte(10,2);

    get_param_remote(5,&FusionParams[0]);
	for(nI=0;nI<64;nI++){
		xil_printf("param[%d] = %d \r\n",nI,FusionParams[nI]);
	}

}
